package com.voute.Presenter.Impl;

import com.voute.Consts.Constants;
import com.voute.Model.Bean.BaseBean;
import com.voute.Model.Bean.UxTagBean;
import com.voute.Model.IResetPasswordModel;
import com.voute.Model.IUxTagModel;
import com.voute.Model.Impl.ResetPasswordModelImpl;
import com.voute.Model.Impl.UXTagModelImpl;
import com.voute.Presenter.IResetPasswordPresenter;
import com.voute.Utils.CommonSubscriber;
import com.voute.Utils.RSAUtils;
import com.voute.Utils.TransformUtils;
import com.voute.View.IView.IForgetPasswordView;

import rx.Observable;
import rx.functions.Action0;
import rx.functions.Func1;

/**
 * Created by liucxu on 2017/3/4.
 */

public class ResetPasswordPresenterImpl implements IResetPasswordPresenter {

    private IForgetPasswordView resetPasswordView;
    private IUxTagModel tagModel;
    private IResetPasswordModel resetPasswordModel;

    public ResetPasswordPresenterImpl(IForgetPasswordView reset_password_view) {
        resetPasswordView = reset_password_view;
        tagModel = new UXTagModelImpl();
        resetPasswordModel = new ResetPasswordModelImpl();
    }

    @Override
    public void getSMSCode(final String phone) {
        tagModel.getUxTag().flatMap(new Func1<UxTagBean, Observable<BaseBean>>() {
            @Override
            public Observable<BaseBean> call(UxTagBean uxTagBean) {
                return resetPasswordModel.getSMSCode(phone, uxTagBean.getData().getUxtag());
            }
        }).compose(TransformUtils.<BaseBean>defaultSchedulers()).doOnSubscribe(new Action0() {
            @Override
            public void call() {
                resetPasswordView.showLoading();
            }
        }).subscribe(new CommonSubscriber<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response) {
                resetPasswordView.hideLoading();
                resetPasswordView.setRightVeritificationCode();
            }

            @Override
            public void onFailure(BaseBean response) {
                resetPasswordView.hideLoading();
                resetPasswordView.setErrorVeritificationCode(response);
            }
        });
    }

    @Override
    public void resetPassword(final String sms_code, final String password, final String phone) {
        tagModel.getUxTag().flatMap(new Func1<UxTagBean, Observable<BaseBean>>() {
            @Override
            public Observable<BaseBean> call(UxTagBean uxTagBean) {
                String encrypiedPassword= RSAUtils.encrypyByPublicKey(password.getBytes(), Constants.getRsaKey());
                return resetPasswordModel.resetPassword(sms_code, encrypiedPassword, phone, uxTagBean.getData().getUxtag());
            }
        }).compose(TransformUtils.<BaseBean>defaultSchedulers()).doOnSubscribe(new Action0() {
            @Override
            public void call() {
                resetPasswordView.showLoading();
            }
        }).subscribe(new CommonSubscriber<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response) {
                resetPasswordView.hideLoading();
                resetPasswordView.doResetPasswordSuccess();
            }

            @Override
            public void onFailure(BaseBean response) {
                resetPasswordView.hideLoading();
                resetPasswordView.doResetPasswordFailure(response);
            }
        });
    }
}
